package com.tc.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

public class RSAUtils {
	public static final int KEYSIZE = 512;

	/**
	 * 解密
	 * @param privateKey 私钥
	 * @param endata 需要解密的内容
	 * @return
	 * @throws Exception
	 */
	public String decrypttoStr(Key privateKey,String endata) throws Exception{
		RandUtil rand = new RandUtil();
		String data = new String(privateEncode(privateKey,rand.parseHexStr2Byte(endata)));
		return data;
	}
 
    /**
     * 解密的方法，使用私钥进行解密
     * privateKey  私钥
     * encoData 需要解密的数据
     * @throws Exception
     */
    public static byte[]  privateEncode(Key privateKey,byte[] encoData) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
        //设置为解密模式，用私钥解密
         cipher.init(Cipher.DECRYPT_MODE, privateKey);
         //解密
         byte[] data = cipher.doFinal(encoData);
//         System.out.println("解密后的数据："+data);
         return data;
    }
    


    /**
	 * 自动生成密钥对
	 * @throws Exception
	 */
	public  Map<String,Object> createKey(){
		 	try {
		        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
		        SecureRandom random = new SecureRandom();
		        keyPairGenerator.initialize(RSAUtils.KEYSIZE, random);
		        // 生成钥匙对
		        KeyPair keyPair = keyPairGenerator.generateKeyPair();
		        // 得到公钥
		        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		        // 得到私钥
		        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
		        Map<String,Object> map = new HashMap<String, Object>();
		        map.put("publicKey", publicKey);
		        map.put("privateKey", privateKey);
		        return map;
		 	} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	}
    
    /**
     * 使用模和指数生成RSA私钥
     * /None/NoPadding】
     * @param modulus
     *            模
     * @param exponent
     *            指数
     * @return
     */
    public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {
        try {
            BigInteger b1 = new BigInteger(modulus);
            BigInteger b2 = new BigInteger(exponent);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
            RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
            return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}